package cn.pxs.controller;

import cn.pxs.entity.Order;
import cn.pxs.entity.OrderLine;
import cn.pxs.service.OrdService;
import cn.pxs.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/fore/ord")
public class ForeOrdController {
    @Autowired
    OrdService ordService;

    @RequestMapping("/findItemInfoByOrdid/{ordid}")
    @ResponseBody
    //绑定 地址栏 ordid
    public Map findItemInfoByOrdid(@PathVariable("ordid")Integer ordid){
        //查询 集合书本及订单明细,订单头
        return RespJson.pack
                (200,"查询该订单号的数据",ordService.findItemInfoByOrdid(ordid));
    }

    @RequestMapping("/findInfoByOrdid/{ordid}")
    @ResponseBody
    //绑定地址栏ordid
    public Map findInfoByOrdid(@PathVariable("ordid")Integer ordid){
        //查询 单本 书本及订单明细，订单头
        Object user =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return RespJson.pack
                (200,"查询该订单号的数据",ordService.findInfoByOrdid(user,ordid));
    }

    @RequestMapping("/addAddr")
    @ResponseBody
    //参数需要 JSON格式
    public Map addAddr(@RequestBody Order order){
        if (order.getOrdid()==null||order.getAddr()==null || order.getPhone()==null){
            return RespJson.pack(404,"ID,地址,电话不能为空",null);
        }
        ordService.updateAddr(order);//修改收货地址
        return RespJson.pack(200,"添加地址成功",null);
    }

    @ResponseBody
    @RequestMapping("/addPay")
    //参数需要 JSON格式
    public Map addPay(@RequestBody Order order){
        //修改付款状态，及刷新时间
        if (order.getOrdid()==null){
            return RespJson.pack(404,"ID,付款状态不能为空",null);
        }
        Order o = ordService.updatePay(order);
        return RespJson.pack(200,"付款状态成功",o);
    }

    @RequestMapping("/findOrdLines")
    @ResponseBody
    //以正常的 键值对方式
    public Map findOrdLines(OrderLine orderLine,
                            String tstart,
                            String tend){
        //查找该登录用户的所有订单头信息
        Object user =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return RespJson.pack(200,"你自己的订单信息",ordService.findOrdLines(user,orderLine,tstart,tend));
    }

    @RequestMapping("/findOrdLineByPage/{start}/{num}")
    @ResponseBody
    //绑定 地址栏 start 和 num
    public Map findOrdLineByPage(@PathVariable("start")Integer start,
                            @PathVariable("num")Integer num,
                            String tstart,
                            String tend,
                            OrderLine orderLine){
        //查找该登录用户的所有订单头信息 分页版
        Map map = new HashMap();
        map.put("start",start);map.put("num",num);map.put("tstart",tstart);
        map.put("tend",tend);
        Object user =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Map data = new HashMap();
        data.put("total",ordService.findOrdLineByPageLine(user,orderLine,map));
        data.put("orders", ordService.findOrdLineByPage(user,orderLine,map));
        return RespJson.pack(200,"",data);
    }




}
